/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.report;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 
 * AbstractReportBean records some infomantions for the project.
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 */
public abstract class AbstractReportBean extends PropertyAwareObject implements PropertyChangeListener{
	private ArrayList children = new ArrayList();
	private AbstractReportBean parent;
	private IExpAttributeHandler expAtrributeHandler;
	private HashMap expAttributes = new HashMap();
	public AbstractReportBean(){
		super();
	}

	public AbstractReportBean(AbstractReportBean parent){
		this.parent = parent;
		
	}
	
	public AbstractReportBean getParent(){
		return this.parent;
	}

	public ArrayList getChildren() {
		return children;
	}

	public void setChildren(ArrayList children) {
		this.children = children;
	}
	
	public void addChildren(AbstractReportBean rb){
		if(this.children.contains(rb)){
			return;
		}
		this.children.add(rb);
		rb.setParent(this);
		rb.addPropertyChangeListener(this);
	}

	public void setParent(AbstractReportBean parent) {
		this.parent = parent;
	}
	
	public void addExpAttributeHandler(IExpAttributeHandler propertyHandler){
		this.expAtrributeHandler = propertyHandler;
	}
	
	public void removeExpAttributeHandler(){
		this.expAtrributeHandler = null;
	}
	
	public IExpAttributeHandler getExpAtrributeHandler(){
		return this.expAtrributeHandler;
	}
	
	public HashMap getExpAttributes() {
		return expAttributes;
	}

	public void setExpAttributes(HashMap expAttributes) {
		this.expAttributes = expAttributes;
	}
	
	public void putExpAttributes(HashMap expAttributes){
		if(this.expAttributes!=null){
			this.expAttributes.putAll(expAttributes);
		}else{
			this.expAttributes = expAttributes;
		}
		this.firePropertyChange(PROPERTY_EXP, null, this.expAttributes);
	}

	public void putExpAttribute(String key, String value) {
		if (this.expAttributes == null) {
			this.expAttributes = new HashMap();
		}
		this.expAttributes.put(key, value);
		this.firePropertyChange(PROPERTY_EXP, null, this.expAttributes);
	}

	public String getExpAttribute(String key) {
		if (this.expAttributes != null) {
			return String.valueOf(this.expAttributes.get(key));
		}
		return null;
	}
	public void propertyChange(PropertyChangeEvent evt) {
		String propertyName = evt.getPropertyName();
		Object value = evt.getNewValue();
		Object obj = evt.getSource();
		if (PropertyAwareObject.PROPERTY_EXP.equals(propertyName)) {
			IExpAttributeHandler propertyHandler = this
					.getExpAtrributeHandler();
			if (propertyHandler == null) {
				return;
			}			
			this.putExpAttributes(propertyHandler.handlerExpAttributes(obj,value));		
		}
	}
}
